from PyQt5.Qt import *
from resource.login_ui import Ui_Form
from API.API_Tool import APITools


class LoginPane(QWidget, Ui_Form):

    success_login = pyqtSignal(str)

    def __init__(self, parent=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.setupUi(self)
        self.refresh()

    def refresh(self):
        print("刷新验证码")
        yzm = APITools.download_yzm()
        # 设置验证码图片属性
        pixmap = QPixmap(yzm)
        self.yzm_label.setPixmap(pixmap)

    # def auto_dm(self):
    #     print("自动打码")

    def login(self):
        print("登录")
        username = self.username.text()
        password = self.password.text()
        coordinate = self.yzm_label.get_coordinate()
        if APITools.check_yzm(coordinate):
            print("验证码正确")
            # passcode = APITools.get_slide_passcode(username)
            # APITools.check_username_password(username, password, passcode)
        else:
            print("验证码错误")
            self.yzm_label.clear_points()
            self.refresh()
        name = APITools.login()
        print(name)
        self.success_login.emit(name)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    login = LoginPane()
    login.show()

    sys.exit(app.exec())
